//
//  UIView+HKAnimation.m
//  HKProject
//
//  Created by 胡锦涛 on 2020/5/29.
//  Copyright © 2020 胡锦涛. All rights reserved.
//

#import "UIView+HKAnimation.h"

@implementation UIView (HKAnimation)
- (void)hk_animation_blinkUpAndDown:(void (^)(void))complete {
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    CABasicAnimation *scaleAni = [CABasicAnimation animation];
    scaleAni.keyPath = @"transform.scale.y";
    scaleAni.toValue = @(0.00);
    group.duration = 0.3;
    group.animations = @[scaleAni];
    [group setValue:@"toMini" forKey:@"animType"];
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:group forKey:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:0.5 animations:^{
            CAAnimationGroup *group = [CAAnimationGroup animation];
            CABasicAnimation *scaleAni = [CABasicAnimation animation];
            scaleAni.keyPath = @"transform.scale.y";
            scaleAni.toValue = @(1.00);
            group.duration = 0.4;
            group.animations = @[scaleAni];
            [group setValue:@"toMini" forKey:@"animType"];
            group.removedOnCompletion = NO;
            group.fillMode = kCAFillModeForwards;
            [self.layer addAnimation:group forKey:nil];
        } completion:^(BOOL finished) {
            if (complete) {
                sleep(1);
                complete();
            }
        }];
    });
    
}

- (void)hk_animation_shake:(void (^)(void))complete {
    CAKeyframeAnimation *rvibrateAni = [CAKeyframeAnimation animation];
    rvibrateAni.keyPath = @"transform.rotation";
    CGFloat angle = M_PI_4/18;
    rvibrateAni.values = @[@(-angle),@(angle),@(-angle)];
    rvibrateAni.repeatCount = MAXFLOAT;
    rvibrateAni.duration = 0.25;
    [self.layer addAnimation:rvibrateAni forKey:@"shake"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.layer removeAnimationForKey:@"shake"];
        if (complete) {
            complete();
        }
    });
}
- (void)hk_animation_rotate:(void (^)(void))complete{
    CATransition *animation = [CATransition animation];
    animation.type = @"rotate";
    animation.subtype = @"fromTop";
    animation.duration = 1.1;
    [animation setValue:@"rotate" forKey:@"animType"];
    [self.layer addAnimation:animation forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hidden = YES;
        if (complete) {
            complete();
        }
    });
}

- (void)hk_animation_suckEffect:(void (^)(void))complete{
    CATransition *animation = [CATransition animation];
    animation.type = @"suckEffect";
    animation.subtype = @"fromTop";
    animation.duration = 1.3;
    [animation setValue:@"suckEffect" forKey:@"animType"];
    [self.layer addAnimation:animation forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hidden = YES;
        if (complete) {
            complete();
        }
    });
}
- (void)hk_animation_cube:(void (^)(void))complete{
    CATransition *animation = [CATransition animation];
    animation.type = @"cube";
    animation.subtype = @"fromTop";
    animation.duration = 1.1;
    [animation setValue:@"cubeEffect" forKey:@"animType"];
    [self.layer addAnimation:animation forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hidden = YES;
        if (complete) {
            complete();
        }
    });
}
- (void)hk_animation_oglFlip:(void (^)(void))complete{
    CATransition *animation = [CATransition animation];
    animation.type = @"oglFlip";
    animation.subtype = @"fromTop";
    animation.duration = 1.1;
    [animation setValue:@"oglFlip" forKey:@"animType"];
    [self.layer addAnimation:animation forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hidden = YES;
        if (complete) {
            complete();
        }
    });
    
}
/** 弹簧动画，先缩小后放大 */
- (void)hk_animation_springEffect:(void (^)(void))complete {
    // 先缩小
    self.transform = CGAffineTransformMakeScale(0.7, 0.7);
    
    // 弹簧动画，参数分别为：时长，延时，弹性（越小弹性越大），初始速度
    [UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:0 animations:^{
        // 放大
        self.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        if (complete) {
            complete();
        }
    }];
}

#pragma mark---------伸缩变换------
//弹出
- (void)hk_popOutsideWithDuration:(NSTimeInterval)duration {
    
    self.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 3.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(1.5f, 1.5f); // 放大
        }];
        [UIView addKeyframeWithRelativeStartTime:1/3.0 relativeDuration:1/3.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(0.8f, 0.8f); // 放小
        }];
        [UIView addKeyframeWithRelativeStartTime:2/3.0 relativeDuration:1/3.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //恢复原样
        }];
    } completion:nil];
}
//弹进
- (void)hk_popInsideWithDuration:(NSTimeInterval)duration {
    
    self.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 2.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(0.7f, 0.7f); // 放小
        }];
        [UIView addKeyframeWithRelativeStartTime:1/2.0 relativeDuration:1/2.0 animations: ^{
            self.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //恢复原样
        }];
    } completion:nil];
}

- (void)hk_cellFlipping {
    [UIView transitionWithView:self duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil];
}
- (void)hk_startDuangAnimation {
    UIViewAnimationOptions op = UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionBeginFromCurrentState;
    [UIView animateWithDuration:0.15 delay:0 options:op animations:^{
        [self.layer setValue:@(0.80) forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 delay:0 options:op animations:^{
            [self.layer setValue:@(1.3) forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.15 delay:0 options:op animations:^{
                [self.layer setValue:@(1) forKeyPath:@"transform.scale"];
            } completion:NULL];
        }];
    }];
}

- (void)hk_startTransitionAnimation {
    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [self.layer addAnimation:transition forKey:nil];
}
@end
